﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.SinhVienService;
namespace eTMS.Controllers{
public class SinhVienControllerBase : CommonController{
	public SinhVienControllerBase()
		: base()
	{	
		this.dao = new SinhVienServiceClient();
	}
	~SinhVienControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private SinhVienEntity oSinhVien;	
	public SinhVienEntity SinhVienObject{
		get{
			return oSinhVien;
		}
		set{
			oSinhVien=value;
		}
	}
	protected SinhVienServiceClient dao;
	public DataTable Table{
		get{
			return _Table;
		}
        set
        {
            _Table = value;
        }
	}
	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oSinhVien==null)return false;
		bool result=true;
		try{
			oSinhVien = dao.SaveRet(oSinhVien);
		}
		catch{
			result=false;
		}
		return result;
	}
	public bool Update(){
		if(oSinhVien==null)return false;
		return dao.Update(oSinhVien);
	}
	public bool Delete(){
		if(oSinhVien==null)return false;
		bool retvalue = dao.Delete(oSinhVien);
		return retvalue;
	}
	public bool DeleteByID(Int32 SinhVien_ID){
		bool retvalue = dao.DeleteByID(SinhVien_ID);		
		return retvalue;
	}
	public void ResetObject(){
		this.oSinhVien = new SinhVienEntity();
	}
	//Check if exists MaSinhVien
	public bool CheckExistsMaSinhVien(object value){	
		return dao.SelectOneByMaSinhVien(value.ToString()).MaSinhVien!=null;
    }    
    
    //Copy data from data row in grid to object
    public SinhVienEntity MapDataRowToObject(DataRow row){
		SinhVienEntity o = new SinhVienEntity();
		 o.MaSinhVien=Convert.ToString(row["MaSinhVien"]);
		 o.TenSinhVien=Convert.ToString(row["TenSinhVien"]);
		 o.DiaChi=Convert.ToString(row["DiaChi"]);
		 o.Email=Convert.ToString(row["Email"]);
		 o.SoCMND=Convert.ToString(row["SoCMND"]);
		 o.DienThoai=Convert.ToString(row["DienThoai"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("GioiTinh"))
			o.GioiTinh=ConvertByte(row["GioiTinh"]);
		 o.NgaySinh=Convert.ToString(row["NgaySinh"]);
		 if(!row.IsNull("SinhVien_ID"))
		    o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(SinhVienEntity o,DataRow row){
		row["MaSinhVien"]=o.MaSinhVien;
		row["TenSinhVien"]=o.TenSinhVien;
		row["DiaChi"]=o.DiaChi;
		row["Email"]=o.Email;
		row["SoCMND"]=o.SoCMND;
		row["DienThoai"]=o.DienThoai;
		row["GhiChu"]=o.GhiChu;
		row["GioiTinh"]=o.GioiTinh;
		row["NgaySinh"]=o.NgaySinh;
		row["SinhVien_ID"]=o.SinhVien_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.SinhVienObject,row);
    }
	public void LoadObject(Int32 SinhVien_ID){
			oSinhVien=this.dao.SelectOne(SinhVien_ID);		
			
	}
}
}
